package com.faintlight.controller;

import com.faintlight.model.Philosophy;
import com.faintlight.service.PhilosophyService;
import com.faintlight.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/philosophies")
@Tag(name = "处世哲学", description = "处世哲学管理接口")
@RequiredArgsConstructor
public class PhilosophyController {

    private static final Logger logger = LoggerFactory.getLogger(PhilosophyController.class);

    private final PhilosophyService philosophyService;
    private final UserService userService;

    @PostMapping
    @Operation(summary = "保存新的处世哲学")
    public ResponseEntity<Philosophy> savePhilosophy(@RequestBody Philosophy philosophy, @RequestHeader("X-User-Id") Long userId) {
        philosophy.setUser(userService.getUserById(userId));
        Philosophy saved = philosophyService.savePhilosophy(philosophy);
        return ResponseEntity.ok(saved);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户的处世哲学")
    public ResponseEntity<List<Philosophy>> getUserPhilosophies(@PathVariable Long userId) {
        List<Philosophy> philosophies = philosophyService.getUserPhilosophies(userId);
        return ResponseEntity.ok(philosophies);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除处世哲学")
    public ResponseEntity<Map<String, String>> deletePhilosophy(@PathVariable Long id, @RequestHeader("X-User-Id") Long userId) {
        philosophyService.deletePhilosophy(id, userId);
        Map<String, String> response = new HashMap<>();
        response.put("message", "处世哲学删除成功");
        return ResponseEntity.ok(response);
    }
}